package net.sf.tools.uiconfig.tags.table;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.DynamicAttributes;

import net.sf.tools.uiconfig.tags.AbstractViewTag;

public class CellTag extends AbstractViewTag implements DynamicAttributes{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2376409169342438588L;

	private Map<String, String> attributes = new HashMap<String, String>();

	private String uiFieldName;
	private String targetFieldName;
	
	public CellTag() {
		// TODO Auto-generated constructor stub
	}

	public String getUiFieldName() {
		return uiFieldName;
	}

	public void setUiFieldName(String uiFieldName) {
		this.uiFieldName = uiFieldName;
	}

	public String getTargetFieldName() {
		return targetFieldName;
	}

	public void setTargetFieldName(String targetFieldName) {
		this.targetFieldName = targetFieldName;
	}


	public void setDynamicAttribute(String arg0, String key, Object value)
			throws JspException {
		if(isDynamicAttribute(key))
			attributes.put(key.toLowerCase(), String.valueOf(value));
		
	}
	

	@Override
	public int doEndTag() throws JspException {
		boolean populate = populateTag(getHttpServletRequest(), getUiFieldName(), getTargetFieldName());
		if(populate){
			writeEndTag();
			return EVAL_PAGE;
		} 
		return SKIP_PAGE;
	}

	

	@Override
	public int doStartTag() throws JspException {
		boolean populate = populateTag(getHttpServletRequest(), getUiFieldName(), getTargetFieldName());
		if(populate){
			try {
				pageContext.getOut().write(buildStartHtml(attributes));
			} catch (IOException e) {
				pageContext.getServletContext().log(e.getLocalizedMessage(), e);
			}
	
			return EVAL_BODY_INCLUDE;
		}
		return SKIP_PAGE;
	}
	
}
